/**
 * The Trucoder.SearchPanel global namespace
 * @class Trucoder.SearchPanel
 * @static
 * @description This class is used for selecting search criteria
 */
if (typeof Trucoder == "undefined") {
    Trucoder = {};
}

Trucoder.SearchPanel={
/**
 * @param {Boolean} isPanelShowing Is the pannel currently showing
 */
    isPanelShowing:false,
/**
 * Init component
 */
    init:function(){
        //Center panel on the screen
        //@todo  Support for multiple platforms
        window.sizeToContent();
        var centerY=(screen.availHeight - window.outerHeight)/2
        var centerX=((screen.availLeft + screen.availWidth - window.outerWidth) - 10)/2
        window.moveTo(centerX ,centerY);
        this.initTabs();
        Footbar.Plugins.init();
    },


    initTabs:function(){

        //Bind the FreeText and Tag Based Search Tabs
        var CustomTab_SearchFreeText= {
            index:0,
            preventDefault:true,
            panelName:'CustomTab_SearchFreeText',
            onClick:function(){
                var deck_1=document.getElementById('trucoder-panels-search-tabs-deck-1');
                deck_1.style.visibility="";

                var deck_2=document.getElementById('trucoder-panels-search-tabs-deck-2');
                deck_2.style.visibility="";
                deck_2.style.display="none"

                window.sizeToContent();
            }
        }
        //trucoder-panels-search-tabs-deck
        Footbar.TabWatcher.bindSingleTab('trucoder-panels-search-tabs', CustomTab_SearchFreeText);

        //Bind the FreeText and Tag Based Search Tabs
        var CustomTab_SearchTagGrid = {
            index:1,
            preventDefault:true,
            panelName:'CustomTab_SearchTagGrid',
            onClick:function(){
                var deck_1=document.getElementById('trucoder-panels-search-tabs-deck-1');
                deck_1.style.visibility="";

                var deck_2=document.getElementById('trucoder-panels-search-tabs-deck-2');
                deck_2.style.visibility="";
                deck_2.style.display=""

                Trucoder.GridUtil.parseXML2KeywordsForGrid('grid-tags-search');
                window.sizeToContent();
            }
        }
        //trucoder-panels-search-tabs-deck
        Footbar.TabWatcher.bindSingleTab('trucoder-panels-search-tabs', CustomTab_SearchTagGrid);
        Footbar.TabWatcher.initalize();
    },

/**
 * @Description Let everyone know that we are performing a search
 *
 */
    notifySearchObservers:function(){
        toggleSidebar('viewEmptySidebar', true);
        var documentSidebar=window.parent.window.opener.document;
        var sidebarWindow = documentSidebar.getElementById("sidebar").contentWindow;
        var iCheckForExistence=0
        iCheckForExistence=setInterval (function(){
            //wait for it to be completaly loaded
            if(sidebarWindow  && sidebarWindow.Trucoder  && sidebarWindow.Trucoder.SearchService.isLoaded){
                clearInterval(iCheckForExistence);
                dump("\n notifySearchObservers- found target window =" + iCheckForExistence)   
                dump("\nsidebarWindow.Trucoder.SearchService=" + sidebarWindow.Trucoder.SearchService.doSearchKeywords );

                //Make sure that the window is loaded then we can notify all
                var checkBoxes=document.getElementById('grid-tags-search')
                var checkBoxes=checkBoxes.getElementsByTagName('checkbox');

                var searchType=document.getElementById('grid-tags-search-type');
                var keywords=[];
                var data={};
                for(var k=0;k<checkBoxes.length;k++){
                    var box=checkBoxes[k];
                    if(box.checked){
                        keywords.push(box.getAttribute("value"));
                    }
                }
                data.keywords=keywords;
                data.searchType=searchType.value;
                var jdata=JSON.stringify ( data );
                sidebarWindow.Trucoder.SearchService.doSearchKeywords  ( jdata );
                
                var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
                observerService.notifyObservers(this, "trucoder-search-request", jdata);


                clearInterval(iCheckForExistence);
            }
            sidebarWindow = documentSidebar.getElementById("sidebar").contentWindow;
        },50)


    }
}



//chrome://browser/conten/browser.js
// |forceOpen| is a bool that indicates that the sidebar should be forced open.  In other words
// the toggle won't be allowed to close the sidebar.
function toggleSidebar(aCommandID, forceOpen) {
    //Since We are opening it from a dialog we need to get the parent of the Dialog
    var document=window.parent.window.opener.document;

    var sidebarBox = document.getElementById("sidebar-box");

    if (!aCommandID)
        aCommandID = sidebarBox.getAttribute("sidebarcommand");

    var elt = document.getElementById(aCommandID);
    var sidebar = document.getElementById("sidebar");
    var sidebarTitle = document.getElementById("sidebar-title");
    var sidebarSplitter = document.getElementById("sidebar-splitter");

    if (!forceOpen && elt.getAttribute("checked") == "true") {
        elt.removeAttribute("checked");
        sidebarBox.setAttribute("sidebarcommand", "");
        sidebarTitle.setAttribute("value", "");
        sidebarBox.hidden = true;
        sidebarSplitter.hidden = true;
        content.focus();
        return;
    }

    var elts = document.getElementsByAttribute("group", "sidebar");
    for (var i = 0; i < elts.length; ++i)
        elts[i].removeAttribute("checked");

    elt.setAttribute("checked", "true");;

    if (sidebarBox.hidden) {
        sidebarBox.hidden = false;
        sidebarSplitter.hidden = false;
    }

    var url = elt.getAttribute("sidebarurl");
    var title = elt.getAttribute("sidebartitle");
    if (!title)
        title = elt.getAttribute("label");
    sidebar.setAttribute("src", url);
    sidebarBox.setAttribute("src", url);
    sidebarBox.setAttribute("sidebarcommand", elt.id);
    sidebarTitle.setAttribute("value", title);
}
